﻿using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;

namespace Tatibah
{
    /// <summary>
    /// A Cell of a Table
    /// </summary>
    public class Cell
    {

        private Table parent = null;

        /// <summary>
        /// How the Cell contents are aligned vertically
        /// </summary>
        public TableVerticalAlignmentValues VerticalAlignment = TableVerticalAlignmentValues.Top;


        /// <summary>
        /// Preferred cell width. See Width Type for Width units. Default units fifieths of a percent.
        /// If Column Width is set then it is used as the default width
        /// </summary>
        public double Width = 0;

        /// <summary>
        /// Set the units for cell width. Default units fifieths of a percent
        /// </summary>
        public TableWidthUnitValues WidthType = TableWidthUnitValues.Pct; // Default is percent


        /// <summary>
        /// Set the background colour of the Cell
        /// </summary>
        public  string BackgroundColour = "";

        /// <summary>
        /// The number of columns this Cell will be merged across
        /// </summary>
        public int ColumnSpan = 0;

        /// <summary>
        /// The number of Rows the Cell will be merged down
        /// </summary>
        public int RowSpan = 0;

        private List<CellContent> contents = new List<CellContent>();

        /// <summary>
        /// Construct an empty Cell, notify Cell who the Parent Table is
        /// </summary>
        /// <param name="Parent">Parent table</param>
        public Cell(Table Parent)
        {
            parent = Parent;
        }


        #region P R O P E R T I E S

        /// <summary>
        /// A List of the Cells contents
        /// </summary>
        public List<CellContent> Contents
        {
            get { return contents; }
        }

       
      

        #endregion P R O P E R T I E S

        /// <summary>
        /// Append Multiple Items to a Cell. Items can be Paragraphs, Images, NumberedLists and Tables
        /// </summary>
        /// <param name="MultipleItems"></param>
        public void Append(params iCellAppend[] MultipleItems)
        {
            foreach (iCellAppend item in MultipleItems)
            {
                if (item.GetType() == typeof(Paragraph))
                {

                    CellContent content = new CellContent((Paragraph) item);
                    contents.Add(content);
                }
                else if (item.GetType() == typeof(Table))
                {
                    if (parent == (Table)item)
                    {
                        throw new Exception("Cannot Append a Table to itself");
                    }
                    CellContent content = new CellContent((Table)item);
                    contents.Add(content);
                }
                else if (item.GetType() == typeof(Image))
                {
                    CellContent content = new CellContent((Image)item);
                    contents.Add(content);
                }
                else if (item.GetType() == typeof(NumberedList))
                {
                    CellContent content = new CellContent((NumberedList)item);
                    contents.Add(content);
                }
            }
        }
     
        /// <summary>
        /// Append a string to a Cell. The string is converted to a paragraph
        /// </summary>
        /// <param name="MultipleStrings"></param>
        /// <returns></returns>
        public void Append(params string[] MultipleStrings)
        {
            foreach (string item in MultipleStrings)
            {
                Paragraph P = new Paragraph(item);
                CellContent c = new CellContent(P);
                contents.Add(c);
            }
           
            
        }
    }
}
